﻿// Implements functions that allow an element to load content or data
define([
    'Wjs/data/RemoteLoader',
    'Wjs/data/LocalLoader'
],
function () {
    'use strict';

    return Wjs.defineOnce({

        type: 'Loadable',

        internal: {

            listeners: {

                onCloned: function (cloned) {

                    this.copyLoadableTo(cloned);
                }
            }
        },

        // Sets the loader to load the element
        setLoader: function (loader) {
            var me = this;

            me.loader = loader

                .setScope(me)

                .onData(function (data) {

                    me.loadData(data);
                });

            return me;
        },

        onBeforeLoad: function (fcn) {

            this.beforeLoadHandler = fcn; // Not chaining since it might create confusion with returning false to cancel the load

            //this.beforeLoadHandler = Wjs.chainQuitOnFalse(this.beforeLoadHandler, fcn);

            return this;
        },

        onLoad: function (fcn) {

            this.loadHandler = Wjs.chain(this.loadHandler, fcn);

            return this;
        },

        onAfterLoad: function (fcn) {

            this.afterLoadHandler = Wjs.chain(this.afterLoadHandler, fcn);

            return this;
        },

        // Called by the loader when the data is loaded
        loadData: function (data, contentType) {

            if (!contentType) { // Default to JSON

                contentType = 'json';
            }

            this.loadHandler(data, contentType);

            if (this.afterLoadHandler) {

                this.afterLoadHandler();
            }

            return this;
        },

        getLoader: function () {

            return this.loader;
        },

        load: function (callback) {

            if (!this.loader) { // No need to continue since the element is not doing anything with the loaded data

                console.log('Type: ' + this.type + ' does not have a loader set ... Quitting the load');

                return;
            }

            if (!this.loadHandler) { // No need to continue since the element is not doing anything with the loaded data

                console.log('Type: ' + this.type + ' is not handling loading data ... Quitting the load');

                return;
            }

            if (this.beforeLoadHandler) {

                if (this.beforeLoadHandler() === false) { // Save a server trip if we do not want to load

                    return;
                }
            }

            this.loader.getData(callback);

            return this;
        },

        copyLoadableTo: function (view) {
            var loader = this.loader.clone();

            view.setLoader(loader);

            view.beforeLoadHandler = this.beforeLoadHandler;

            view.loadHandler = this.loadHandler;

            view.afterLoadHandler = this.afterLoadHandler
        }

    });

});